import requests, json, traceback

# 会话管理
session = requests.session()

# 获取token
result = session.post('http://testingedu.com.cn:8081/inter/HTTP/auth')
print(result.text)
jsonres = json.loads(result.text)

# 添加键值对到头里面
session.headers['token'] = jsonres['token']

# 键值对格式的参数，在python里面需要用字典传
# 如果是json字符串，就直接传json=字符串
params = {
    'username': 'Will',
    'password': '123456'
}

result = session.post('http://testingedu.com.cn:8081/inter/HTTP/login', data=params)
# text,content对应response
print(result.text)
# result.headers对应response header
# 对应request headers
# print(result.request.headers)
# result.status_code对应statu code

jsonres = json.loads(result.text)
try:
    expectres = '{"msg":"恭喜您，登录成功","userid":"1"}'
    expectres = json.loads(expectres)
    for key in expectres.keys():
        if expectres[key] != jsonres[key]:
            print("FAIL")
            break

except Exception as e:
    print('FAIL')
    print(traceback.format_exc())

result = session.post('http://testingedu.com.cn:8081/inter/HTTP/logout')
# text,content对应response
print(result.text)
# import requests
#
# requests.post('http://testingedu.com.cn:8081/inter/HTTP/register',data='username=Tester55&pwd=123456&nickname=测试账号&describe=这个账号，是一个测试注册的账号')